import 'package:flutter/material.dart';
import 'package:flutter_frontend/main.dart';

class MyNavigatorRoute {
  MyNavigatorRoute._();

  /// 跳转到某一页
  static void push({BuildContext? context, required Widget toPage, void Function(dynamic result)? callBack}) {
    if (context == null) {
      final BuildContext? current = navigatorKey.currentContext;
      if (current != null) {
        Navigator.push(current, MaterialPageRoute(builder: (BuildContext context) => toPage)).then((result) {
          if (callBack != null) callBack(result);
        });
      }
    } else {
      Navigator.push(context, MaterialPageRoute(builder: (BuildContext context) => toPage)).then((result) {
        if (callBack != null) callBack(result);
      });
    }
  }

  /// 返回上一路由
  static void pop(BuildContext context, [dynamic result]) {
    Navigator.pop(context, result);
  }

  /// 返回到根路由
  static void popToRoot(BuildContext context) {
    Navigator.popUntil(context, ModalRoute.withName('/'));
  }
}
